

import java.io.*;

public class MyBookStore {


  public static void main (String[] args) throws IOException {

    // declare variables
    int menucode;
    BookStore abookstore;
    Book abook;
    String title;
    int num_pages;
    double price;
    int quantity;
    int num;

    // create the BufferedReader object for input
    BufferedReader stdin =
          new BufferedReader(new InputStreamReader(System.in));

    // create a book store
    abookstore = new BookStore();

    // Give the menu to user until he/she quits
    do {
      System.out.println("\n  BookStore Menu \n" +
                         "  ============== \n" +
                         "  1 to add a book to the stock \n" +
                         "  2 to sell a book in stock \n" +
                         "  3 to list the titles of the books in stock \n" +
                                                                                                 "  4 to list all information about the books in stock\n" +
                         "  5 to print out the gross income of the store \n" +
                         "  6 to quit");
      System.out.print("  Enter a menu code > ");
      System.out.flush();
      menucode = Integer.parseInt(stdin.readLine().trim());
      switch (menucode) {
        case 1: // add a book to the stock
          System.out.print("\nEnter the title of the book to be added : ");
          System.out.flush();
          title = stdin.readLine();
          if (abookstore.inStock(title,0)) {
            // the book is in stock
            System.out.println("The book is in the stock ");
            System.out.print("\nEnter how many extra books to stock: ");
            System.out.flush();
            num = Integer.parseInt(stdin.readLine().trim());
            abookstore.addBookQuantity(title,num);
          }
          else {
            System.out.println("The book is NOT in the stock ");
            System.out.print("\nEnter number of pages : ");
            System.out.flush();
            num_pages = Integer.parseInt(stdin.readLine().trim());
            System.out.print("Enter quantity : ");
            System.out.flush();
            quantity = Integer.parseInt(stdin.readLine().trim());
            System.out.print("Enter price : ");
            System.out.flush();
            price = Double.parseDouble(stdin.readLine().trim());
            abookstore.addNewBook(new Book(title,num_pages,price,quantity));
          }
          break;
        case 2: // sell a book in stock
          System.out.print("\nEnter the title of the book to be sold : ");
          System.out.flush();
          title = stdin.readLine();
          if (abookstore.inStock(title,0)) {
            // the book is in stock
            System.out.println("The book is in the stock ");
            System.out.print("\nEnter how many books to be sold: ");
            System.out.flush();
            num = Integer.parseInt(stdin.readLine().trim());
            if (abookstore.inStock(title,num))
              // there is enough copies of the book in stock
              abookstore.sellBook(title,num);
            else
              System.out.println("There are not enough copies of the book in stock.");
          }
          else
            System.out.println("The book is NOT in the stock ");
          break;
        case 3: // list all the book titles in stock
          abookstore.listTitles();
          break;
                                case 4: // list all the information about the books in stock
                                        abookstore.listBooks();
                                        break;
        case 5: // print out the gros income of the store
          System.out.println("\nThe totol gross income of the bookstore : " +
                             abookstore.getIncome());
          break;
        case 6: // quit
          break;
        default:
          System.out.println("\nIllegal Menu Code -- you have to enter one of 1, 2, 3, 4, 5 or 6");
      }
    } while (menucode != 6);

  }
}
